﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 获取WMS托库存信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMSBarcodeKCController : ControllerBase
    {
        private readonly ILogger<BXWMSBarcodeKCRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con2;


        public WMSBarcodeKCController(ILogger<BXWMSBarcodeKCRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("WMSDbConnection_ZH");

        }

        /// <summary>
        /// 获取WMS托库存信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWMSBarcodeKC([FromBody] BXWMSBarcodeKCRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            BXWMSBarcodeKCResponseDto re = new BXWMSBarcodeKCResponseDto();
            string ddate = strparm.ddate;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {

                strSql = new StringBuilder();
                strSql.Append(@"delete from barcode_temp_bxfh");
                strSqlList.Add(strSql.ToString());
                if (strSqlList.Count > 0)
                {
                    bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                    if (A ==false)
                    {
                        strMsg = "数据库更新失败，请稍后重试";
                    }
                }


                strSqlList = new List<string>();
                strSql = new StringBuilder();
                strSql.Append(@"insert into barcode_temp_bxfh  select * from barcode_file where warehouse_code in ('JWZGP01','JWZGP02','JWZGP03','JWZGP04') and
material_code like '14%' and barcode_status=4 and barcode_type=40 and material_name like '%A+%'");
                strSqlList.Add(strSql.ToString());
                if (strSqlList.Count > 0)
                {
                    bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                    if (A ==false)
                    {
                        strMsg = "数据库更新失败，请稍后重试";
                    }
                }

                strSql = new StringBuilder();
                strSql.Append(@"select * from barcode_temp_bxfh");
                DataTable dt_wmstkc = DB.Query(strSql.ToString(), con);
                if (dt_wmstkc.Rows.Count>0)
                {                    
                    re.data = DB.TableToListModel<BXWMSBarcodeKCResponseData>(dt_wmstkc);
                }
                else
                {
                    strMsg = "获取库存失败，请稍后重试";
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
